package com.imm.da.main.weather.api

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.qweather.sdk.bean.base.Code
import com.qweather.sdk.bean.base.Lang
import com.qweather.sdk.bean.base.Range
import com.qweather.sdk.bean.geo.GeoBean
import com.qweather.sdk.view.QWeather

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2021/12/1
 */
class HeCityService constructor(val context : Context) {

    /**
     * 热门城市查询
     */
    fun hotCity(result : MutableLiveData<List<GeoBean.LocationBean>>){
        QWeather.getGeoTopCity(context, object : QWeather.OnResultGeoListener {
            override fun onSuccess(bean: GeoBean?) {
                if (Code.OK == bean?.code){
                    result.value = bean.locationBean
                }
            }
            override fun onError(t: Throwable?) {
            }
        })
    }

    /**
     * 查询城市列表
     */
    fun cityList(
        result : MutableLiveData<List<GeoBean.LocationBean>>,
        number : Int = 20,
        location : String? = "北京",
        lang : Lang? = Lang.ZH_HANS
    ){
        /**
         * location/必选/需要查询地区的名称，支持文字、以英文逗号分隔的经度,纬度坐标（十进制）、LocationID或Adcode（仅限中国城市）。例如 location=北京 或 location=116.41,39.92
         * adm/可选/城市的上级行政区划，默认不限定行政区划，例如 adm=beijing
         * range/可选/搜索范围，例如world(全球)，cn(中国)，us(美国)
         * number/可选/返回结果的数量，取值范围1-20，默认返回10个结果
         * lang/可选/多语言设置，默认中文，例如zh-hans、zh(简体中文ZH_HANS)，zh-hant(繁体中文ZH_HANT)，en(英文ENGLISH)，ko(韩语KOREAN)，ja(日语JAPANESE)
         */
        QWeather.getGeoCityLookup(context, location, Range.CN, number, lang, object : QWeather.OnResultGeoListener {
            override fun onSuccess(bean: GeoBean?) {
                if (Code.OK == bean?.code){
                    result.value = bean.locationBean
                }
            }
            override fun onError(t: Throwable?) {
            }
        })
    }
}